package leetcodecompetition.D337;

import org.junit.jupiter.api.Test;

public class T6322 {
    public boolean checkValidGrid(int[][] grid) {
        if (grid[0][0]!=0)return false;
        int n = grid.length;
        int[][] ints = new int[n * n][2];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                ints[grid[i][j]][0] = i;
                ints[grid[i][j]][1] = j;
            }
        }
        int a = ints[0][0];
        int b = ints[0][1];
        for (int i = 1; i < ints.length; i++) {
            int c = ints[i][0];
            int d = ints[i][1];
            int di=Math.max(a,c)-Math.min(a,c);
            int dj=Math.max(b,d)-Math.min(b,d);
            if (di==1&&dj==2||di==2&&dj==1){
                a=c;
                b=d;
            }else {
                return false;
            }
        }
        return true;
    }
    @Test
    public void test() {
        int[][]ints={{0,11,16,5,20},{17,4,19,10,15},{12,1,8,21,6},{3,18,23,14,9},{24,13,2,7,22}};
        System.out.println(checkValidGrid(ints));
    }
}
